import path from 'path'
import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
import eslintPlugin from 'vite-plugin-eslint'
import type { ConfigEnv } from 'vite'

// https://vitejs.dev/config/
const viteConfig = defineConfig((mode: ConfigEnv) => {
  const root = process.cwd()
  const env: any = loadEnv(mode.mode, root)
  const { VITE_PORT } = env
  return {
    plugins: [
      vue(),
      vueJsx(),
      eslintPlugin({
        include: [
          'src/**/*.ts',
          'src/**/*.js',
          'src/**/*.vue',
          'src/*.ts',
          'src/*.js',
          'src/*.vue'
        ]
      })
    ],
    define: {
      'process.env': {} // 将上面的env赋值到这里
    },
    server: {
      host: true,
      port: VITE_PORT
    },
    root: process.cwd(), // 项目根目录
    resolve: {
      alias: {
        '@': path.resolve(__dirname, './src'),
        '@yang': path.resolve(__dirname, './src/library')
      }
    },
    css: {
      preprocessorOptions: {
        scss: {
          additionalData: '@import "@/theme/index.scss";'
        }
      }
    }
  }
})

export default viteConfig
